1777E - Edge Reverse - CodeForces Solution


binary search dfs and similar graphs

Please click on ads to support us..

C++ Code:

// 2023-01-22
#include <bits/stdc++.h>
#define fastio                    \
	ios_base::sync_with_stdio(0); \
	cin.tie(0);
#define vi vector<int>
#define vl vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vp vector<pi>
#define vpl vector<pl>
#define ll long long
#define MAX 2147000000
#define MOD 1000000007
using namespace std;

int main(){
	fastio;
    int t;
    cin >> t;
    while(t--){
        int n, m;
        cin >> n >> m;
        vector<vp> g(n + 1);
        for(int i{0}; i < m; ++i){
            int a, b, c;
            cin >> a >> b >> c;
            g[a].push_back({b, c});
        }
        int l{0}, r{(int)1e9};
        int ans{-1};
        while(l <= r){
            int mid{(l + r) / 2};
            vector<vi> g2(n + 1);
            for(int i{1}; i <= n; ++i){
                for(auto& j : g[i]){
                    g2[i].push_back(j.first);
                    if(j.second <= mid){                        
                        g2[j.first].push_back(i);
                    }
                }
            }  
            vi visited(n + 1);
            int root; 
            function<void(int)> dfs = [&](int cur){
                for(auto& i : g2[cur]){
                    if(visited[i] == 0){
                        visited[i] = 1;
                        dfs(i);
                    }
                }
                root = cur;
            };
            for(int i{1}; i <= n; ++i){
                if(!visited[i]){
                    visited[i] = 1;
                    dfs(i);
                }
            }
            vi ch(n + 1);
            ch[root] = 1;
            queue<int> Q;
            int cnt{0};
            Q.push(root);
            while(!Q.empty()){
                int x{Q.front()};
                Q.pop();
                cnt++;
                for(auto& i : g2[x]){
                    if(ch[i] == 0){
                        ch[i] = 1;
                        Q.push(i);
                    }
                }
            }
            if(cnt == n){
                ans = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        cout << ans << "\n";
    }
}


Comments

Submit
0 Comments
More Questions

1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation